package model

import (
	"github.com/jmoiron/sqlx"
	"time"
	. "zimuzu_cvn_web_api/pkg/e"
)

//数据库
type TransferNotice struct {
	Id              int64   `db:"id" json:"id"`
	NoticeId        string  `db:"notice_id" json:"notice_id"`
	Coin            string  `db:"coin" json:"coin"`
	AbsAmount       float64 `db:"abs_amount" json:"abs_amount"`
	Amount          int64   `db:"amount" json:"amount"`
	Decimal         int64   `db:"decimal" json:"decimal"`
	From            string  `db:"from" json:"from"`
	To              string  `db:"to" json:"to"`
	TxId            string  `db:"txid" json:"txid"`
	TransStatus     string  `db:"trans_status" json:"trans_status"`
	Status          int64   `db:"status" json:"status"`
	RequestNum      int64   `db:"request_num" json:"request_num"`
	NextRequestTime int64   `db:"next_request_time" json:"next_request_time"`
	CreateTime      int64   `db:"create_time" json:"create_time"`
	UpdateTime      int64   `db:"update_time" json:"update_time"`
}

//添加记录
func AddTransferNotice(db sqlx.Execer, data TransferNotice) error {
	nowTime := time.Now().Unix()
	data.Status = 0
	data.RequestNum = 0
	data.NextRequestTime = nowTime
	data.CreateTime = nowTime
	data.UpdateTime = nowTime
	_, err := db.Exec("INSERT INTO transfer_notice (notice_id, coin, abs_amount, amount, `decimal`, `from`, `to`, txid, trans_status, `status`, request_num, next_request_time, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
		data.NoticeId, data.Coin, data.AbsAmount, data.Amount, data.Decimal, data.From, data.To, data.TxId, data.TransStatus, data.Status, data.RequestNum, data.NextRequestTime, data.CreateTime, data.UpdateTime)
	if err != nil {
		Log("err", err)
	}
	return err
}

//修改记录
func UpdateTransferNotice(db sqlx.Execer, data TransferNotice) error {
	data.UpdateTime = time.Now().Unix()
	_, err := db.Exec("UPDATE transfer_notice SET trans_status = ?, `status` = ?, request_num = request_num + 1, next_request_time = ?,  update_time = ? WHERE notice_id = ?",
		data.TransStatus, data.Status, data.NextRequestTime, data.UpdateTime, data.NoticeId)
	if err != nil {
		Log("err", err)
	}
	return err
}

//获取未处理记录列表
func GetTransferNoticeWaitList(db sqlx.Queryer, coin string) (qi []TransferNotice, err error) {
	err = sqlx.Select(db, &qi, "SELECT notice_id,request_num from transfer_notice WHERE `status` = 0 AND coin = ? AND next_request_time <= ? ORDER BY id ASC LIMIT ?",
		coin, time.Now().Unix(), 20)
	if err != nil {
		Log("err", err)
	}
	return qi, err
}

//检查NoticeId是否存在
func ExistTransferNoticeId(db sqlx.Queryer, noticeId string) (bool, error) {
	var count int
	err := sqlx.Get(db, &count,"SELECT count(*) FROM transfer_notice WHERE notice_id = ?", noticeId)
	if err != nil {
		Log("err", err)
		return false, err
	}
	if count == 0 {
		return false, nil
	}
	return true, nil
}

//检查TxId是否存在
func ExistTransferNoticeTxId(db sqlx.Queryer, txId string) (bool, error) {
	var count int
	if txId == "" {
		return false, nil
	}
	err := sqlx.Get(db, &count,"SELECT count(*) FROM transfer_notice WHERE txid = ?", txId)
	if err != nil {
		Log("err", err)
		return false, err
	}
	if count == 0 {
		return false, nil
	}
	return true, nil
}